

function openProjectTab(element) {
	var projectName = element.getAttribute('data-project-name');
	var projectId = element.getAttribute('data-project-id');
	$.modal.open(projectName, '/project/detail/' + projectId);
}

function openIssueTab(issueId) {
	$.modal.openTab('需求详情 #' + issueId, '/project/issue/' + issueId);
}

function claimIssue(id){
	layer.confirm('确定要领取该需求吗？', {
	    icon: 3,
	    title: '确认领取'
	}, function(index) {
	    $.ajax({
	        url: prefix + "/claimIssue/" + id,
	        type: "GET",
	        beforeSend: function() {
	            layer.load(2);
	        },
	        success: function(result) {
	            layer.closeAll();
	            if (result.code == 0) {
	                layer.msg("领取成功！", {icon: 1});
	                // 刷新表格数据
	                $.table.refresh();
	            } else {
	                layer.msg("领取失败：" + result.msg, {icon: 2});
	            }
	        },
	        error: function() {
	            layer.closeAll();
	            layer.msg("领取请求失败", {icon: 2});
	        }
	    });
	    layer.close(index);
	});
}

/**
 * 提交需求
 */
function submitIssue(id) {
	// 弹出提交弹窗
	layer.open({
		type: 1,
		title: '提交需求',
		area: ['500px', '300px'],
		content: '<div style="padding: 20px;">' +
			'<div class="form-group">' +
			'<label>提交说明：</label>' +
			'<textarea id="submitComment" class="form-control" rows="6" placeholder="请输入提交说明..." style="resize: none;"></textarea>' +
			'</div>' +
			'<div class="text-center" style="margin-top: 20px;">' +
			'<button type="button" class="btn btn-primary" onclick="confirmSubmit(' + id + ')">确认提交</button>' +
			'&nbsp;&nbsp;&nbsp;&nbsp;' +
			'<button type="button" class="btn btn-default" onclick="layer.closeAll()">取消</button>' +
			'</div>' +
			'</div>'
	});
}

/**
 * 确认提交
 */
function confirmSubmit(id) {
	var comment = $('#submitComment').val().trim();
	
	layer.confirm('确定要提交该需求吗？', {
		icon: 3,
		title: '确认提交'
	}, function(index) {
		$.ajax({
			url: prefix + "/submitIssue/" + id,
			type: "POST",
			data: {
				comment: comment
			},
			beforeSend: function() {
				layer.load(2);
			},
			success: function(result) {
				layer.closeAll();
				if (result.code == 0) {
					layer.msg("提交成功！", {icon: 1});
					// 刷新表格数据
					$.table.refresh();
				} else {
					layer.msg("提交失败：" + result.msg, {icon: 2});
				}
			},
			error: function() {
				layer.closeAll();
				layer.msg("提交请求失败", {icon: 2});
			}
		});
		layer.close(index);
	});
}

/**
 * 审核需求
 */
function reviewIssue(id) {
	// 弹出审核弹窗
	layer.open({
		type: 1,
		title: '审核需求',
		area: ['500px', '300px'],
		content: '<div style="padding: 20px;">' +
			'<div class="form-group">' +
			'<label>审核意见：</label>' +
			'<textarea id="reviewComment" class="form-control" rows="6" placeholder="请输入审核意见..." style="resize: none;"></textarea>' +
			'</div>' +
			'<div class="text-center" style="margin-top: 20px;">' +
			'<button type="button" class="btn btn-success" onclick="submitReview(' + id + ', 1)">通过</button>' +
			'&nbsp;&nbsp;&nbsp;&nbsp;' +
			'<button type="button" class="btn btn-danger" onclick="submitReview(' + id + ', 0)">不通过</button>' +
			'&nbsp;&nbsp;&nbsp;&nbsp;' +
			'<button type="button" class="btn btn-default" onclick="layer.closeAll()">取消</button>' +
			'</div>' +
			'</div>'
	});
}

/**
 * 提交审核结果
 */
function submitReview(id, status) {
	var comment = $('#reviewComment').val().trim();
	
	if (status == 0 && !comment) {
		layer.msg('请输入审核意见', {icon: 2});
		return;
	}
	
	var statusText = status == 1 ? '通过' : '不通过';
	layer.confirm('确定要' + statusText + '该需求吗？', {
		icon: 3,
		title: '确认' + statusText
	}, function(index) {
		$.ajax({
			url: prefix + "/reviewIssue/" + id,
			type: "POST",
			data: {
				status: status,
				comment: comment
			},
			beforeSend: function() {
				layer.load(2);
			},
			success: function(result) {
				layer.closeAll();
				if (result.code == 0) {
					layer.msg("审核" + statusText + "成功！", {icon: 1});
					// 刷新页面
					location.reload();
				} else {
					layer.msg("审核失败：" + result.msg, {icon: 2});
				}
			},
			error: function() {
				layer.closeAll();
				layer.msg("审核请求失败", {icon: 2});
			}
		});
		layer.close(index);
	});
}



// 关注/取消关注函数
function toggleWatch(issueId) {
	// 先检查当前状态
	$.ajax({
		url: prefix + "/isWatched/" + issueId,
		type: "GET",
		dataType: "json",
		success: function(result) {
			if (result.code == 0) {
				var isWatched = result.data;
				if (isWatched) {
					// 当前已关注，执行取消关注
					unwatchIssue(issueId);
				} else {
					// 当前未关注，执行关注
					watchIssue(issueId);
				}
			} else {
				$.modal.alertError("获取关注状态失败");
			}
		},
		error: function() {
			$.modal.alertError("获取关注状态失败");
		}
	});
}

// 关注问题
function watchIssue(issueId) {
	$.ajax({
		url: prefix + "/watch/" + issueId,
		type: "POST",
		dataType: "json",
		success: function(result) {
			if (result.code == 0) {
				// 更新按钮状态
				updateWatchButton(true);
				$.modal.msgSuccess(result.msg);
			} else {
				$.modal.msgError(result.msg);
			}
		},
		error: function() {
			$.modal.msgError("关注失败，请稍后再试");
		}
	});
}

// 取消关注问题
function unwatchIssue(issueId) {
	$.ajax({
		url: prefix + "/unwatch/" + issueId,
		type: "POST",
		dataType: "json",
		success: function(result) {
			if (result.code == 0) {
				// 更新按钮状态
				updateWatchButton(false);
				$.modal.msgSuccess(result.msg);
			} else {
				$.modal.msgError(result.msg);
			}
		},
		error: function() {
			$.modal.msgError("取消关注失败，请稍后再试");
		}
	});
}

// 更新关注按钮状态
function updateWatchButton(isWatched) {
	var watchButton = $('#watchButton');
	var watchIcon = $('#watchIcon');
	var watchText = $('#watchText');
	
	if (isWatched) {
		// 已关注状态
		watchButton.removeClass('btn-default').addClass('btn-primary');
		watchIcon.removeClass('fa-eye').addClass('fa-eye-slash');
		watchText.text('取消关注');
	} else {
		// 未关注状态
		watchButton.removeClass('btn-primary').addClass('btn-default');
		watchIcon.removeClass('fa-eye-slash').addClass('fa-eye');
		watchText.text('关注');
	}
}

// 加载关注者列表
function loadWatchers(issueId) {
	$.ajax({
		url: prefix + "/watchers/" + issueId,
		type: "GET",
		dataType: "json",
		success: function(result) {
			if (result.code == 0) {
				displayWatchers(result.data);
			} else {
				$('#watchers-list').html('<div class="text-center"><small class="text-muted">加载失败</small></div>');
			}
		},
		error: function() {
			$('#watchers-list').html('<div class="text-center"><small class="text-muted">加载失败</small></div>');
		}
	});
}

// 显示关注者列表
function displayWatchers(watchers) {
	var watcherList = $('#watchers-list');
	var watcherCount = $('#watcher-count-num');
	
	if (watchers && watchers.length > 0) {
		var html = '';
		watchers.forEach(function(watcher) {
			html += '<div class="sidebar-watcher-item">';
			html += '<i class="fa fa-user text-muted"></i> ';
			html += '<span class="watcher-user-name">' + (watcher.userName || '未知用户') + '</span>';
			html += '</div>';
		});
		watcherList.html(html);
		watcherCount.text(watchers.length);
	} else {
		watcherList.html('<div class="text-center"><small class="text-muted">暂无关注者</small></div>');
		watcherCount.text('0');
	}
}
